<?php
$vAllTrees = AFT_MakeTreeDB('seed');	
global $icons;
$vSettings = array();
$vSettings = AFT_load('settings');

$wccnt=0;
$mscnt=0;

$vGiftboxContent=AllForTrees_GetGiftboxContent();

$building = $vGiftboxContent['building'];

foreach ($building as $bldng) {
	if ($bldng['name'] == 'mysteryseedling') {
		$mscnt=$mscnt+1;
	} elseif ($bldng['name'] == 'wateringcan'){
		$wccnt=$wccnt+1;
	}
}

$vxWaters = array();
foreach ($building as $bldng) {
	if ($bldng['name'] == 'wateringcan'){
		$vxWaters[] = $bldng;
	}
}
$buildings =GetObjects('MysterySeedling');

?>
<div class="settings">
	<table class="settings">
		<tbody>
			<tr>
				<td>
					<b><?=$mscnt?></b> mystery seedlings in giftbox &ndash;
					Place <input maxlength="5" name="settings-Seedplacecnt" class="saveConf" size="3" type="text" value="<?=intval($vSettings['Seedplacecnt'])?>"> of them.<br>
					Or: <input name="settings-autoPlaceSeedlings" class="saveConf" type="checkbox"<?=$vSettings['autoPlaceSeedlings']?" checked":""?>> Automatically place seedlings,
					but keep <input name="settings-autoPlaceSeedlingsKeep" class="saveConf" size="4" value="<?=$vSettings['autoPlaceSeedlingsKeep']?>"><br>
				</td>
			</tr>
			<tr>
				<td>
					Default action for...
					<div style="text-align:right">
						...un-mastered seedlings:
						<select name="settings-seedUnmasteredAction" class="saveConf">
							<option value="" <?=$vSettings['seedUnmasteredAction'] == ''?" selected":""?> >do nothing</option>
							<option value="sell" <?=$vSettings['seedUnmasteredAction'] == 'sell'?" selected":""?> >sell</option>
							<option value="water" <?=$vSettings['seedUnmasteredAction'] == 'water'?" selected":""?> >water</option>
						</select><br>
						...mastered seedlings:
						<select name="settings-seedMasteredAction" class="saveConf">
							<option value="" <?=$vSettings['seedMasteredAction'] == ''?" selected":""?> >do nothing</option>
							<option value="sell" <?=$vSettings['seedMasteredAction'] == 'sell'?" selected":""?> >sell</option>
							<option value="water" <?=$vSettings['seedMasteredAction'] == 'water'?" selected":""?> >water</option>
						</select>
					</div>
				</td>
			</tr>
			<tr>
				<td>
					<b><?=$wccnt?></b> watering cans (for &gt;= <?php echo floor($wccnt / 8);?> seedlings) in giftbox<br>
					Water seedlings 
					<select name="actn" size="1" onchange="fAction('faction=Seedwater&val=',this.value)" >
						<option value="3" <?= $vSettings['Seedwater'] == 3 ? ' selected':'' ?> >to 3</option>
						<option value="7" <?= $vSettings['Seedwater'] == 7 ? ' selected':'' ?> >to 7</option>
						<option value="8" <?= $vSettings['Seedwater'] == 8 ? ' selected':'' ?> >fully (to 8)</option>
					</select><br>
					But keep <input name="settings-keepWateringcans" class="saveConf" size="4" value="<?=$vSettings['keepWateringcans']?>"> watering cans.
				</td>
			</tr>
		</tbody>
	</table>
	<button onclick="$('#seedTable > tbody > tr > td[count]').each(function(i,e) { $(e).find('input[name=keep]').val(999).change(); $(e).parent().find('select').val($(e).parent().hasClass('mastered')?'sell':'water').change();}); ">Water all unmastered<br>and Sell all mastered</button>
</div>

<div class="maintable">		
	<table class="grid" width="600" id="seedTable">
		<caption><?= count($buildings) ?> Seedlings on the farm</caption>
		<thead>
			<tr>
				<th width="60">Image</th>
				<th>Name</th>
				<th>Trees</th>
				<th>Mastery</th>
				<th>Coins</th>
				<th>Coconuts</th>
				<th>Jade</th>
				<th>Seedlings</th>
				<th>Action</th>
			</tr>
		</thead>
		<tbody>

<?php

$ofcnt=0;
foreach ($vAllTrees as $vATr) {
	if ($vATr['cntSeed']>0) {
		$ofcnt = $ofcnt + $vATr['cntSeed'];
		$class = $vATr['mastery'] >= 1?"mastered":"";
		echo "\n\n\t<tr tree=\"{$vATr['code']}\" class=\"$class\">";
		echo AFT_tr($vATr,array('icon','name','count','mastery','coins','cocos','jade'));
?>
		<td><?=$vATr['cntSeed']?></td>
		<td class="colorme" count="<?=$vATr['cntSeed']?>">
			<img class="icon<?=$vATr['actionSeed']=="water"?" on":""?>" src="<?=$icons['water'.($vATr['actionSeed']=="water"?"":"Off")]?>" title="water" action="actionSeed">
			<img class="icon<?=$vATr['actionSeed']=="sell"?" on":""?>" src="<?=$icons['sell'.($vATr['actionSeed']=="sell"?"":"Off")]?>" title="sell" action="actionSeed">
			<br>
			Count:&nbsp;<input maxlength="6" name="keep" size="3" type="text" value="<?=$vATr['actionSeedcnt']?>"  
							onchange="fAction('faction=actionSeedcnt&code=<?=urlencode($vATr['code'])?>&val=',this.value)" title="-1 for all">
		</td>
	</tr>
<?php
	}
}

?>
</tbody>
<tfoot>
<?php

  if (count($buildings) - $ofcnt > 0){
?>
		
		<tr tree="mysterySeedling"><td width="60"><image width="60" height="60" src="<?php echo Image_GetSrcByName('mysteryseedling') ?>"></td>
		<td style="text-align:left;">Unknown</td>
		<td>?</td>
		<td>?</td>
		<td>?</td>
		<td>?</td>
		<td>?</td>
		<td><?=intval(count($buildings) - $ofcnt)?></td>
		<td class="colorme" count="<?=intval(count($buildings) - $ofcnt)?>">
			<img class="icon<?=$vSettings['Seedunk']=="water"?" on":""?>" src="<?=$icons['water'.($vSettings['Seedunk']=="water"?"":"Off")]?>" title="water" action="Seedunk">
			<img class="icon<?=$vSettings['Seedunk']=="sell"?" on":""?>" src="<?=$icons['sell'.($vSettings['Seedunk']=="sell"?"":"Off")]?>" title="sell" action="Seedunk">
			<br>
			Count:&nbsp;<input maxlength="6" name="keep" size="3" type="text" value="<?=$vSettings['Seedunkcnt']?>"  
							onchange="fAction('faction=Seedunkcnt&val=',this.value)" >
		</td>
		</tr>
<?php
	}
?>
		<tr>
			<th>&nbsp;</th>
			<th><b>Total</b></th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
			<th><?= count($buildings) ?></th>
			<th>&nbsp;</th>
		</tr>
	</tfoot>
</table>
</div>
